﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace UI.Admin.Role
{
    /// <summary>
    /// AjaxAction1 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
           // string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "RoleAdmin", "");
            //if (auth != "ACCEPT")
           // {
           //     return;
           // }

            context.Response.ContentType = "text/plain";

            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = "test";//((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = "1314";//context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加角色
            if (dowhat == "add_role")
            {
                string del = "  ";
                string Name = context.Request["Name"].Trim(del.ToCharArray());
                if (Name.Length < 1)
                {
                    context.Response.Write("角色名称不能为空（包括空格）");
                    return;
                }
                string RoleInfoModelID = context.Request["RoleInfoModelID"];
                string IsAdmin = context.Request["IsAdmin"];
                string RoleEnabled = context.Request["RoleEnabled"];

                string ModelAdmin = context.Request["ModelAdmin"];
                string ColumnAdmin = context.Request["ColumnAdmin"];
                 string UserAdmin = context.Request["UserAdmin"];
                string RoleAdmin = context.Request["RoleAdmin"];
                string FileAdmin = context.Request["FileAdmin"];
                string LogAdmin = context.Request["LogAdmin"];
                string SystemAdmin = context.Request["SystemAdmin"];
                string ContentAdmin = "";




                if (BLL.Create.CreateRole(Name, RoleInfoModelID,RoleEnabled, IsAdmin, ModelAdmin, UserAdmin, RoleAdmin, LogAdmin,SystemAdmin, ColumnAdmin,  FileAdmin,ContentAdmin) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加了名为‘" + Name + "’角色");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除角色
            if (dowhat == "delete_role")
            {
                string ID = context.Request["ID"];

                string Name = BLL.Find.FindRole(ID).Name;

                if (BLL.Delete.DeleteRole(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除了名为'" + Name + "角色");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 更新角色
            if (dowhat == "update_role")
            {
                string ID = context.Request["ID"];
                string Name = context.Request["Name"];
                string RoleInfoModelID = context.Request["RoleInfoModelID"];
                string IsAdmin = context.Request["IsAdmin"];
                string ModelAdmin = context.Request["ModelAdmin"];
                string ColumnAdmin = context.Request["ColumnAdmin"];
                string ContentAdmin = "";
                string UserAdmin = context.Request["UserAdmin"];
                string RoleAdmin = context.Request["RoleAdmin"];
                string FileAdmin = context.Request["FileAdmin"];
                string LogAdmin = context.Request["LogAdmin"];
                string SystemAdmin = context.Request["SystemAdmin"];
                string RoleEnabled = context.Request["RoleEnabled"];

                if (BLL.Update.UpdateRole(ID,Name, RoleInfoModelID, RoleEnabled, IsAdmin, ModelAdmin, UserAdmin, RoleAdmin, LogAdmin, SystemAdmin, ColumnAdmin, FileAdmin, ContentAdmin) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "更新了名为‘" + Name + "’的角色");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}